package com.bridge.common.http;

import android.app.Activity;
import com.bridge.common.base.BaseAppLication;
import com.bridge.common.dialog.VDialog;
import com.bridge.common.interfaces.IHttpCallback;
import com.bridge.common.utils.NetUtils;
import com.google.gson.JsonSyntaxException;
import java.net.SocketTimeoutException;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.HttpException;


/**
 * 请求网络工具类
 *
 * @author dingyb
 */
public class HttpUtils {

    private static HttpUtils httpUtil;
    private boolean isCancle = true;//是否可中断请求
    Disposable mDisposable = null;
    private HttpUtils() {

    }

    public static HttpUtils getInstance() {
        if (httpUtil == null) {
            synchronized (HttpUtils.class) {
                if (httpUtil == null) {
                    httpUtil = new HttpUtils();
                }
            }
        }
        return httpUtil;
    }
    /**
     * http请求
     *
     * @param requestCode
     */

    public void doHttp(int requestCode, Observable<ResponseBody> call, String lodingStr, IHttpCallback callback) {
        sendRequest(call, requestCode, lodingStr,callback);
    }


    public void sendRequest(Observable<ResponseBody> observable, final int requestCode, String lodingStr, final IHttpCallback callback) {
        if (!NetUtils.isConnected(BaseAppLication.getContext())) {
            if (callback != null) {
                callback.onError(requestCode, "网络错误");
            }
            return;
        }
        if (callback != null) {
            callback.onLoading(requestCode, lodingStr);
        }

        observable.subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        cancelProgressDialog();
                        try {
                            String result = responseBody.string();
                            if (result == null || "".equals(result)) {
                                return;
                            }
                            if (callback != null) {
                                callback.onSuccess(requestCode, result);
                            }
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                            if (callback != null) {
                                callback.onFailure(requestCode, "数据解析异常");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        mDisposable.dispose();
                    }

                    @Override
                    public void onError(Throwable ex) {
                        ex.printStackTrace();
                        cancelProgressDialog();
                        if (callback != null) {
                            callback.onError(requestCode, "网络错误");
                        }
                        mDisposable.dispose();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public void showProgressDialog(Activity context, String lodingStr, boolean isCancle) {
        VDialog.getInstance().showLoadingDialog(lodingStr, context, true, true);
    }

    public void cancelProgressDialog() {
        //DialogUtils.getInstance().dismiss();
        VDialog.getInstance().hideLoadingDialog();
    }

    /**
     * 中断请求
     */
    public void cancle() {
        if (mDisposable == null) {
            return;
        }
        if (!mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }
    public void setCancle(boolean cancle) {
        isCancle = cancle;
    }
    public boolean isCancle() {
        return isCancle;
    }
}

